package com.jiaanpei.frameproject.utils;

import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
    public static final String TAG = "TimeUtil";


    /**把日期转为字符串
     * @param
     * @return
     */
    public static String getDateToString()

    {
        long time = new Date().getTime();


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");

        return df.format(time);

    }








    /**
     * 获取毫秒的long类型time
     *
     * @param server_time
     * @return
     */
    public static long getTimeMillis(String server_time) {
        if (TextUtils.isEmpty(server_time)) {
            return 0;
        }
        long time = 0L;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d;
        try {
            d = sdf.parse(server_time);
            return d.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return time;
    }


    /**
     * 获得当天0点时间
     *
     * @return
     */
    public static long getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 获得昨天0点时间
     *
     * @return
     */
    public static long getTimesYesterday() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.DAY_OF_MONTH - 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    /**
     * 服务器时间与本地时间对比
     *
     * @param server_time
     * @return
     */
    public static String TimeFormat(String server_time) {
        if (TextUtils.isEmpty(server_time)) {
            return "";
        }
        long MINUTE = 60000;
        Calendar currCalendar = Calendar.getInstance();

        long serverTime = getTimeMillis(server_time);

        Calendar serverCalendar = Calendar.getInstance();
        currCalendar.setTimeInMillis(serverTime);

        long currTime = System.currentTimeMillis();
        currCalendar.setTimeInMillis(currTime);

        /**
         * 加一个10秒钟的容错处理
         */
        long diffTime = currTime - serverTime + 10000;
        if (diffTime < 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            String time = sdf.format(new Date(serverTime));
            if (time.contains("年0"))
                time = time.replace("年0", "年");
            if (time.contains("月0"))
                time = time.replace("月0", "月");
            if (time.contains("日 0"))
                time = time.replace("日 0", "日 ");
            return time;
        } else if (diffTime > 0 && diffTime < MINUTE) {
            return "现在";
        } else if (diffTime >= MINUTE && diffTime < MINUTE * 60) {
            SimpleDateFormat sdf = new SimpleDateFormat("mm分钟前");
            String time = sdf.format(new Date(diffTime));
            if (time.startsWith("0"))
                time = time.substring(1);
            return time;
        } else if (diffTime >= MINUTE * 60 && diffTime < MINUTE * 60 * 24
                && serverTime >= getTimesmorning()) {
            SimpleDateFormat sdf = new SimpleDateFormat("今天 HH:mm");
            String time = sdf.format(new Date(serverTime));
            if (time.contains("今天 0"))
                time = time.replace("今天 0", "今天 ");
            return time;
        } else if (diffTime >= MINUTE * 60 && diffTime < MINUTE * 60 * 24 * 2
                && serverTime < getTimesmorning()
                && serverTime >= getTimesYesterday()) {
            SimpleDateFormat sdf = new SimpleDateFormat("昨天 HH:mm");
            String time = sdf.format(new Date(serverTime));
            if (time.contains("昨天 0"))
                time = time.replace("昨天 0", "昨天 ");
            return time;
        } else {
            SimpleDateFormat year = new SimpleDateFormat("yyyy");
            // 如果服务器的年份与本地年份相同 则仅显示月日
            if (year.format(new Date(getTimeMillis(server_time))).equals(
                    year.format(new Date(currTime)))) {
                SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 HH:mm");
                String time = sdf.format(new Date(serverTime));
				/*if (time.startsWith("0"))
					time = time.substring(1);
				if (time.contains("月0"))
					time = time.replace("月0", "月");
				if (time.contains("日 0"))
					time = time.replace("日 0", "日 ");*/
                return time;
            }

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
            String time = sdf.format(new Date(serverTime));
			/*if (time.contains("年0"))
				time = time.replace("年0", "年");
			if (time.contains("月0"))
				time = time.replace("月0", "月");
			if (time.contains("日 0"))
				time = time.replace("日 0", "日 ");*/
            return time;
        }
    }

    /** 上次点击时间 */

    /**
     * 判断是否快速双击
     *
     * @return
     */

    public static Long lastClickTime ;

    public static boolean isFastDoubleClick() {
        try {
            if(lastClickTime==null) {
                lastClickTime = 0L;
            }
        }catch (NumberFormatException e){
            lastClickTime=0L;
        }

        long time = System.currentTimeMillis();
        long dur = time - lastClickTime;
        lastClickTime = time;

        if (dur < 2000) {
            return true;
        }
        return false;
    }


    /**
     * 判断是否快速双击
     *
     * @return
     */
    public static boolean isFastDoubleClick(View v) {
        if(v==null){
            return false;
        }
        Long lastClickTime;
        try {
            lastClickTime = (Long) v.getTag();
            if(lastClickTime==null) {
                lastClickTime = 0L;
            }
        }catch (NumberFormatException e){
            lastClickTime=0L;
        }

        long time = System.currentTimeMillis();
        long dur = time - lastClickTime;

        if (dur < 1000) {
            return true;
        }
        v.setTag(time);
        return false;
    }




    /* * 输入某年某月某日，判断这一天是这一年的第几天？ */
    public static int getDateDays(String date1, String date2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyymmdd");
        try {
            Date date = sdf.parse(date1);// 通过日期格式的parse()方法将字符串转换成日期
            Date dateBegin = sdf.parse(date2);
            long betweenTime = date.getTime() - dateBegin.getTime();
            betweenTime = betweenTime / 1000 / 60 / 60 / 24;
            return (int) betweenTime;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 10000;
    }

    public static void cannoutDoubleClick(final View view){
        view.setClickable(false);
        new Thread(){
            @Override
            public void run() {
                SystemClock.sleep(1500);
                view.setClickable(true);
            }
        }.start();
    }

    public static void cannoutDoubleClick(final ArrayList<View> rbList){
        for (View view: rbList) {
            view.setEnabled(false);
        }

        new Thread(){
            @Override
            public void run() {
                SystemClock.sleep(3000);
                for (View view:
                        rbList) {
                    view.setEnabled(true);
                }
            }
        }.start();
    }
}
